﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net;
using System.Globalization;

namespace BusinessDll
{
    class FTPService
    {
        public void send_image()
        {
            //String fileName = GateMain.img_path;
            String fileName = BusinessControl.pathImg;
            //GateMain.img_path = "";
            FtpWebRequest reqFTP = null;
            string serverIP;
            string userName;
            string password;
            string url;
            //GeneralTools generalTools = new GeneralTools();
            FileStream fs = null;
            Stream strm = null;
            try
            {
                FileInfo fileInf = new FileInfo(fileName);
                serverIP = GlobalInfo.Instance.generalParam["FTPServer"];
                userName = GlobalInfo.Instance.generalParam["FTPUser"];
                password = GlobalInfo.Instance.generalParam["FTPPasswd"];
#if false
                serverIP = generalTools.getConfigByName("GENERAL", "FTPServer");
                userName = generalTools.getConfigByName("GENERAL", "FTPUser");
                password = generalTools.getConfigByName("GENERAL", "FTPPasswd");
#endif
                string folderDate = DateTime.Now.ToString("yyyyMMdd");
                //url = "ftp://" + serverIP + "/" + Path.GetFileName(fileName);
                url = "ftp://" + serverIP + "/" + folderDate + "/" + BusinessControl.sessionID + ".jpg";

                FtpMakeDir(serverIP, folderDate, userName, password);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFTP.Credentials = new NetworkCredential(userName, password);
                reqFTP.KeepAlive = false;

                reqFTP.Timeout = 60000;
                reqFTP.ServicePoint.ConnectionLimit = 10;

                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.ContentLength = fileInf.Length;

                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                strm = reqFTP.GetRequestStream();

                contentLen = fs.Read(buff, 0, buffLength);

                while (contentLen != 0)
                {

                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                WriteLog.printException(ex);
            }
            finally
            {
                Console.WriteLine("图片上传完成");
                if (strm != null) strm.Close();
                if (fs != null) fs.Close();
                if (reqFTP != null)
                {
                    reqFTP.Abort();
                }
            }
        }

        public void FtpMakeDir(String ftpServerIP, String localFile, String userName, string ftpPassword)
        {
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftpServerIP + "/" + localFile);
            reqFTP.Credentials = new NetworkCredential(userName, ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
            FtpWebResponse response = null;
            try
            {
                reqFTP.UsePassive = true;
                reqFTP.UseBinary = false;
                reqFTP.KeepAlive = false;
                response = (FtpWebResponse)reqFTP.GetResponse();

            }
            catch (Exception ex)
            {
                reqFTP.Abort();
            }
            finally
            {
                reqFTP.Abort();
                if (response != null) response.Close();

            }

        }

    }
}
